﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using System.Xml.Serialization;
using WebGioiThieu.Modules.SlideImages;

namespace WebGioiThieu.Areas.Admin.Controllers
{
    public class SlideImagesController : Controller
    {
        //
        // GET: /Admin/SlideImages/

        public ActionResult Index()
        {
            //get

            string path = Server.MapPath("~/Configs/SlideImages.xml");
            var deserializer = new XmlSerializer(typeof(SlideImages));
            var textReader = new StreamReader(path);
            var model = (SlideImages)deserializer.Deserialize(textReader);
            textReader.Close();
            model.SlideImageList = model.SlideImageList.OrderBy(c => c.DisplayOrder).ToList();
            return View(model);

        }

        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(SlideImageItem image)
        {
            //get
            string path = Server.MapPath("~/Configs/SlideImages.xml");
            var deserializer = new XmlSerializer(typeof(SlideImages));
            var textReader = new StreamReader(path);
            var model = (SlideImages)deserializer.Deserialize(textReader);
            textReader.Close();

            model.SlideImageList = model.SlideImageList.ToList();

            //add======
            if (model.SlideImageList.Count==0)
                image.Id = 0;
            else
                image.Id = model.SlideImageList[model.SlideImageList.Count-1].Id + 1;
            if (image.DisplayOrder == null)
                image.DisplayOrder = model.SlideImageList[model.SlideImageList.Count-1].Id + 1;
            model.SlideImageList.Add(image);

            var serializerObj = new XmlSerializer(typeof(SlideImages));
            TextWriter writeFileStream = new StreamWriter(path);
            serializerObj.Serialize(writeFileStream, model);
            writeFileStream.Close();
            //=========
            return RedirectToAction("Index");
        }
        public ActionResult Update(int id)
        {
            string path = Server.MapPath("~/Configs/SlideImages.xml");
            var deserializer = new XmlSerializer(typeof(SlideImages));
            var textReader = new StreamReader(path);
            var model = (SlideImages)deserializer.Deserialize(textReader);
            textReader.Close();

            model.SlideImageList = model.SlideImageList.ToList();
            foreach (var item in model.SlideImageList)
            {
                if (item.Id == id)
                {
                    return View(item);
                }
            }
            return View();
        }
        [HttpPost]
        public ActionResult Update(SlideImageItem image)
        {
            //get
            string path = Server.MapPath("~/Configs/SlideImages.xml");
            var deserializer = new XmlSerializer(typeof(SlideImages));
            var textReader = new StreamReader(path);
            var model = (SlideImages)deserializer.Deserialize(textReader);
            textReader.Close();

            model.SlideImageList = model.SlideImageList.ToList();

            //add======
            for (int i = 0; i < model.SlideImageList.Count(); i++)
            {
                if (model.SlideImageList[i].Id == image.Id)
                {
                    model.SlideImageList[i].Name = image.Name;
                    model.SlideImageList[i].PictureUrl = image.PictureUrl;
                    model.SlideImageList[i].Url = image.Url;
                    model.SlideImageList[i].DisplayOrder = image.DisplayOrder;
                }
            }

            var serializerObj = new XmlSerializer(typeof(SlideImages));
            TextWriter writeFileStream = new StreamWriter(path);
            serializerObj.Serialize(writeFileStream, model);
            writeFileStream.Close();
            //=========
            return RedirectToAction("Index");
        }
        public ActionResult Delete(int id)
        {
            string path = Server.MapPath("~/Configs/SlideImages.xml");
            var deserializer = new XmlSerializer(typeof(SlideImages));
            var textReader = new StreamReader(path);
            var model = (SlideImages)deserializer.Deserialize(textReader);
            textReader.Close();

            model.SlideImageList = model.SlideImageList.ToList();
            foreach (var item in model.SlideImageList)
            {
                if (item.Id == id)
                {
                    return View(item);
                }
            }
            return View();
        }
        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(SlideImageItem img)
        {
            //get
            string path = Server.MapPath("~/Configs/SlideImages.xml");
            var deserializer = new XmlSerializer(typeof(SlideImages));
            var textReader = new StreamReader(path);
            var model = (SlideImages)deserializer.Deserialize(textReader);
            textReader.Close();

            model.SlideImageList = model.SlideImageList.ToList();

            //add======
            //model.SlideImageList.Remove(img);
            for (int i = 0; i < model.SlideImageList.Count(); i++)
            {
                if (model.SlideImageList[i].Id == img.Id)
                {
                    model.SlideImageList.Remove(model.SlideImageList[i]);
                    break;
                }
            }

            var serializerObj = new XmlSerializer(typeof(SlideImages));
            TextWriter writeFileStream = new StreamWriter(path);
            serializerObj.Serialize(writeFileStream, model);
            writeFileStream.Close();
            //=========
            return RedirectToAction("Index");
        }
        //public ActionResult Delete(int id)
        //{
        //    string path = Server.MapPath("~/Configs/SlideImages.xml");
        //    XDocument doc = XDocument.Load(path);
        //    var q = from node in doc.Descendants("SlideImages")
        //            let attr = node.Attribute("Id")
        //            where attr != null && attr.Value == "id"
        //            select node;
        //    q.ToList().ForEach(x => x.Remove());
        //    doc.Save(path);
        //    return RedirectToAction("Index");
        //}
    }
}
